package com.cloudkinto.service.orderback.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cloudkinto.common.BeanConvert;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.entity.FbaOrderBackProductDo;
import com.cloudkinto.dao.FbaOrderBackProductDao;
import com.cloudkinto.service.orderback.FbaOrderBackProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import com.cloudkinto.service.orderback.vo.FbaOrderBackProductAddReq;
import com.cloudkinto.service.orderback.vo.FbaOrderBackProductDetailRes;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author ding
 * @since 2023-08-02
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class FbaOrderBackProductServiceImpl extends ServiceImpl<FbaOrderBackProductDao, FbaOrderBackProductDo> implements FbaOrderBackProductService {
    @Autowired
    private FbaOrderBackProductDao dao;

    @Override
    public SingleResult addInit() {
        return SingleResult.success("");
    }

    @Override
    public Object add(Object req, Long userId) {
        FbaOrderBackProductDo entityDo = new FbaOrderBackProductDo();
        BeanUtils.copyProperties(req, entityDo);
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);
        return handlerAddAfter(entityDo);
    }

    @Override
    public int saveItems(Long id, List<FbaOrderBackProductAddReq> items, Long createBy) {
        for (FbaOrderBackProductAddReq item : items) {
            FbaOrderBackProductDo entityDo = new FbaOrderBackProductDo();
            entityDo.setOrderId(id);
            entityDo.setProductId(item.getProductId());
            entityDo.setFnSku(item.getFnSku());
            entityDo.setCount(item.getCount());
            handlerAddBefore(entityDo, createBy);
            save(entityDo);
        }
        return 0;
    }

    //新增校验
    private void addCheck(Object req) {

    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(FbaOrderBackProductDo entityDo, Long userId) {
        entityDo.setCreateBy(userId);
        entityDo.setCreateTime(new Date());
        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
    }

    //将新增的do转换成要返回的实体
    private Object handlerAddAfter(FbaOrderBackProductDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success("");
    }

    @Override
    public Object update(Object req, Long userId) {
        /*FbaOrderBackProductDo entityDo = dao.selectById(req.getId());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);*/
        return null;
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(FbaOrderBackProductDo entityDo, Long userId) {
        entityDo.setUpdateBy(userId);
        entityDo.setUpdateTime(new Date());
    }

    //做修改之后要执行的业务定制处理
    private Object handlerUpdateAfter(FbaOrderBackProductDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        FbaOrderBackProductDo entityDo = dao.selectById(id);
        handlerDeleteBefore(entityDo, userId);
        return dao.deleteById(id);
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(FbaOrderBackProductDo entityDo, Long userId) {
        entityDo.setDeleteBy(userId);
        entityDo.setDeleteTime(new Date());
    }

    @Override
    public Object detail(Long id) {
        FbaOrderBackProductDo entityDo = dao.selectById(id);
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private Object handlerDetailAfter(Object res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success("");
    }

    @Override
    public List<FbaOrderBackProductDetailRes> getList(Map<String, String> map) {
        QueryWrapper<FbaOrderBackProductDo> wrapper = queryBuild(map);
        List<FbaOrderBackProductDo> list = this.list(wrapper);
        List<FbaOrderBackProductDetailRes> result = handlerListAfter(list);
        return result;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<FbaOrderBackProductDo> wrapper = queryBuild(map);
        IPage<FbaOrderBackProductDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<FbaOrderBackProductDetailRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    @Override
    public List<FbaOrderBackProductDetailRes> getItemListByOrderId(Long id) {
        List<FbaOrderBackProductDo> fbaOrderBackProductDos = dao.selectList(new LambdaQueryWrapper<FbaOrderBackProductDo>()
                .eq(FbaOrderBackProductDo::getOrderId, id));
        return handlerListAfter(fbaOrderBackProductDos);
    }

    //查询分页列表之后对列表数据的处理
    private List<FbaOrderBackProductDetailRes> handlerListAfter(List<FbaOrderBackProductDo> list) {
        List<FbaOrderBackProductDetailRes> result = new ArrayList<>();
        for (FbaOrderBackProductDo fbaOrderBackProductDo : list) {
            FbaOrderBackProductDetailRes fbaOrderBackProductDetailRes = BeanConvert.INSTANCE.fbaBackProductVo(fbaOrderBackProductDo);
            result.add(fbaOrderBackProductDetailRes);
        }
        return result;
    }

    private IPage<FbaOrderBackProductDo> pageInit(Map<String, String> map) {
        IPage<FbaOrderBackProductDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<FbaOrderBackProductDo> queryBuild(Map<String, String> map) {
        QueryWrapper<FbaOrderBackProductDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("keyword", map.get("keyword"));
        }
        return wrapper;
    }


}
